<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/13
 * Time: 10:52
 */

namespace app\api\controller;

use app\api\Common;
use app\model\BbsCard;
use app\model\CollectionCo;
use app\model\CollectionM;
use app\model\Fans;
use app\model\GuessUserbet;
use app\model\UserFeedback;
use think\Request;
use think\Validate;
use app\model\Userinfo;
use app\model\Videolist;

class User extends Common{

//    public function a(){
//       phpinfo();
//    }

    //版本检测
    public function check(){
        check_version();
    }

    /**
     * 发送短信api
     *  get 请求
     */
    public function sendSms(){
        //手机号(phone) | 发送短信类型(smstype) 找回密码为findpwd
        $phone = request()->get('phone');
        $smstype = request()->get('smstype');
        if(empty($phone)){
            return_json(0,'手机号不能为空');
        }
        if($smstype=='findpwd'){
            $userinfo = Userinfo::where('phone',$phone)->find();
            if(empty($userinfo)){
                return_json(0,'该手机号未注册');
            }
        }
        //调取发送验证码
        sendSms($phone);
    }


    /**
     * 用户注册
     * @param
     * @return string
     *  post请求
     */
    public function register(){
        $data = request()->post();
        $validate=Validate::make([
            'phone'=>"require",

            'password'=>"require|length:6,16",
            'code'=>"require",

        ],[
            'password.length'=>'密码长度为6-16位'
        ],
            [
                'phone'=>'手机号',
                'password'=>'密码',
                'code'=>'验证码'
            ]);
        if(!$validate->check($data)){
            return_json(0,$validate->getError());
        }

        $user_id = Userinfo::where('phone', $data['phone'])->column('id');
        if(!empty($user_id)){
            return_json(0,'该手机号已经注册');
        }

//        check_smscode($data['phone'],$data['code']);
        unset($data['code']);                //删除验证码
        $data['ip'] = getip();               //获取到用户的IP
        $data['area'] = getArea($data['ip']);               //获取到用户的IP所属区域
        $data['reg_time'] = time();                 //注册时间
        $daat['lastlogin_time'] = time();           //最近一次登录的时间
        $data['username'] = 'phdyw'.$data['phone'];         //用户名拼接
        $data['salt'] = randomkeys(6);              //密码盐
        $data['password'] = md5($data['password']. $data['salt']);        //密码等于=>md5加密(密码+盐)
//        $data['pt'] = $this->headers['pt'];         //客户端标识 1安卓 2 IOS
//        $data['imei'] = $this->headers['imei'];
        $data['pt'] = 1;         //客户端标识 1安卓 2 IOS
        $data['imei'] = 2;

        //插入数据到用户表
        try{
            $res = Userinfo::create($data);
        }catch (\Exception $e)
        {
            return_json(0,$e->getMessage());
        }
        if($res->id){
            $user_info = Userinfo::where('phone',$data['phone'])->find();
            return_json(1,'注册成功',$user_info);
        }else{
            return_json(0,'注册失败');
        }
    }


    /**
     * 登录
     * @return ApiException|\think\response\Json
     *
     */
    public function login(){
        $phone =  request()->post('phone');
        $pwd =  request()->post('pwd');
        if(empty($phone) && empty($pwd)){
            return_json(0,'手机号或密码不能为空');
        }
        $salt = Userinfo::where('phone',$phone)->value('salt');
        if(empty($salt)){
            return_json(0,'手机号错误');
        }else{
            $user_info = Userinfo::where('phone',$phone)->where('password',md5($pwd.$salt))->find();
            if(!empty($user_info)){
                $lastlogin_time = time();   //用户最后登录时间
                $ip = getip();   //用户最后登录ip
//                $imei = $this->headers('imei');
//                $pt = $this->headers('pt');
                $imei = 1;
                $pt = 2;
                $data = ['lastlogin_time'=>$lastlogin_time,'ip'=>$ip,'imei'=>$imei,'pt'=>$pt];
                $res = Userinfo::where('phone',$phone)->update($data);
                if($res){
                    return_json(1,'登录成功',$user_info);
                }else{
                    return_json(0,'error');
                }
            }else{
                return_json(0,'密码错误');
            }
        }
    }


    /**
     * 忘记密码
     * @return ApiException|\think\response\Json
     */
    public function forgetPwd()
    {
        $phone =  request()->get('phone');
        $pwd =  request()->get('password');
        $code =  request()->get('code');
//        check_smscode($phone,$code);
        $ip = getIp();
        $area = getArea($ip);
        $salt = randomkeys(6);
        $pwd = md5($pwd.$salt);
//        $imei = $this->headers('imei');
//        $pt = $this->headers('pt');

        $imei = 1;
        $pt = 2;

        $data = [
            'ip'=>$ip,
            'area'=>$area,
            'salt'=>$salt,
            'password'=>$pwd,
            'imei'=>$imei,
            'pt'=>$pt
        ];
        try {
            $id = $res = Userinfo::where('phone',$phone)->update($data);
        }catch (\Exception $e){
            return_json(0,$e->getMessage());
        }
        if($id)
        {
            return_json(1,'找回密码成功');
        }else{
            return_json(0,'找回密码失败');
        }
    }

    /**
     * 用户修改个人信息
     * post 请求
     */
    public function modify_information(){

            //获取需要修改的字段值 用户id(id)|手机号(phone)|昵称(username)|头像(head_portrait)|性别(sex);
        $data = \request()->post();
        $head_portrait = uploadImageOne('head_portrait');
        //判断是否有图片上传
        if($head_portrait['status']==0){
            $data['head_portrait'] = $head_portrait['message'];
        }
        $status = Userinfo::where('id',$data['id'])->update($data);
        $user_info = Userinfo::get($data['id']);
        if($status){
            return_json(1,'修改成功',$user_info);
        }else{
            return_json(0,'修改失败',$user_info);
        }
    }



    /**
     * @我的收藏
     */
    public function collection_list(){
        $uid = request()->get('uid');   //获取用户id
        $video_id = CollectionCo::where('uid',$uid)->where('status',0)->column('collection_id');
        $video_list = CollectionM::where('dstatus','<>',3)->where('id','in',$video_id)->paginate(10);
            if(!empty($video_list)){
            foreach ($video_list as $k=>$v){
                $video_list[$k]['is_collect'] = 1;
            }
            return_json(1,'获取收藏视频成功',$video_list);
        }
    }

    /**
     * 删除我的收藏
     *  post请求
     */
    public function del_collection(){

        //用户id(uid)|收藏视频id数组(ids)
        $uid = \request()->post('uid');
        $ids =json_decode(\request()->post('ids'),true);
        $status = CollectionCo::where('uid',$uid)->where('collection_id','in',$ids)->update(['status' => 1]);
        if($status){
            return_json(1,'删除成功');
        }else{
            return_json(0,'删除失败');
        }
    }

    /**
     * 我发表的
     *  get请求
     */
    public function user_release(){

        //用户id(uid)
        $uid = \request()->get('uid');
        $release_list = BbsCard::where(['uid'=>$uid,'dstatus'=>1])->order('id desc')->paginate(6);
        foreach ($release_list as $k=>$v){
            $v['title'] = urldecode($v['title']);
        }
        return_json(1,'获取发布数据成功',$release_list);
    }

    /**
     * 删除我的发表
     * post请求
     */
    public function del_release(){
        //用户id(uid)|发表帖子id数组(ids)
        $uid = \request()->post('uid');
        $ids =json_decode(\request()->post('ids'),true);
        $status = BbsCard::where('uid',$uid)->where('id','in',$ids)->update(['dstatus' => 3]);
        if($status){
            return_json(1,'删除成功');
        }else{
            return_json(0,'删除失败');
        }

    }

    /**
     * 富豪排行
     */
    public function total_rankings(){
        //类型type 1为总金额 | 2为投注
        $type = \request()->get('type');
        if($type==1){
            $rankings_list = Userinfo::where('id','<>','null')->order('total_amount desc')->field('id,username,head_portrait,total_amount')->limit(20)->select();
        }else{
            $rankings_list = GuessUserbet::where('dstatus',1)->field('id,SUM(wager) as total_amount,user_id')->order('total_amount desc')->group('user_id')->limit(20)->select();
            foreach ($rankings_list as $k=>$v){
                $v['username'] = Userinfo::where('id',$v['user_id'])->value('username');
                $v['head_portrait'] = Userinfo::where('id',$v['user_id'])->value('head_portrait');
            }
        }
       return return_json(1,'获取排行成功',$rankings_list);
    }

    /**
     * 用户意见反馈
     */
    public function feedback(){

        //用户id(uid) | 反馈内容(content)
        $uid = \request()->post('uid');
        $content = \request()->post('content');

        $res = UserFeedback::create([
            'uid'=>$uid,
            'content'=>$content,
            'ctime'=>time()
        ]);
        if($res){
            return_json(1,'反馈成功');
        }else{
            return_json(0,'反馈失败');
        }
    }

    /**
     * 我的关注 | 我的粉丝
     */
    public function my_follow(){

        //获取用户id(uid) | 类型(type 1为我关注的|2为的粉丝) | 搜索名称name
        $uid = \request()->get('uid');
        $name = \request()->get('name');
        $type = \request()->get('type');
        $uids = Userinfo::where('username','like','%'.$name.'%')->column('id');
        if($type==1){
            if(empty($name)){
                //查询我关注的
                $ids = Fans::where('uid',$uid)->where('dstatus',1)->column('follow_uid');
            }else{
                $ids = Fans::where('uid',$uid)->where('dstatus',1)->where('follow_uid','in',$uids)->column('follow_uid');
            }
        }else{
            if(empty($name)){
                //查询我的粉丝
                $ids = Fans::where('follow_uid',$uid)->where('dstatus',1)->column('uid');
            }else{
                $ids = Fans::where('follow_uid',$uid)->where('dstatus',1)->where('follow_uid','in',$uids)->column('uid');
            }
        }
        $list = Userinfo::where('id','in',$ids)->field('id,username,head_portrait')->paginate(10);
        foreach ($list as $k=>$v){
            if($type==1){
                $v['is_follow'] = 1;
            }else{
                $ids2 = Fans::where('uid',$uid)->where('dstatus',1)->column('follow_uid');
                if(in_array($v['id'],$ids2)){
                    $v['is_follow'] = 1;
                }else{
                    $v['is_follow'] = 0;
                }
            }
        }
        return_json(1,'获取成功',$list);
    }

    /**
     *  我的  关注|粉丝|发表数
     */
    public function my(){

        $uid = \request()->get('uid');
        $follow = Fans::where(['uid'=>$uid,'dstatus'=>1])->count();
        $fans = Fans::where(['follow_uid'=>$uid,'dstatus'=>1])->count();
        $publish = BbsCard::where(['uid'=>$uid,'dstatus'=>1])->count();

        $result = [
            'follow'=>$follow,
            'fans'=>$fans,
            'publish'=> $publish
        ];
        return_json(1,'获取数据成功',$result);
    }



}